﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemeGo
{
    /// <summary>
    /// 游戏类
    /// </summary>
    class Game
    {
        private const int Cols = 8;
        private const int Rows = 4;
        //private bool IsSame;

        private Block BlockFore;



        public bool IsGameOver;

        private int i = 0;
        private int j = 0;

        /// <summary>
        /// 所有块儿类的集合
        /// </summary>
        private List<Block> Blocks { get; set; }

        /// <summary>
        /// 初始化
        /// </summary>
        public Game()
        {
            //初始化集合
            Blocks = new List<Block>();
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    Blocks.Add(new Block(i, j, (BlockStatus)System.Enum.Parse(typeof(BlockStatus), (j + 1).ToString())));
                }
            }
            Random rnd = new Random();

            //随机排列
            for (int i = 0; i < 1000; i++)
            {
                var index1 = rnd.Next(0, 32);
                var index2 = rnd.Next(0, 32);
                if (index1 != index2)
                {
                    var temp = Blocks[index1].Status;
                    Blocks[index1].Status = Blocks[index2].Status;
                    Blocks[index2].Status = temp;
                }

            }
        }

        /// <summary>
        /// 绘制当前游戏场景
        /// </summary>
        /// <param name="g">绘图句柄</param>
        /// <param name="size">绘制 游戏区域尺寸</param>
        public void Draw(Graphics g, Size size)
        {
            //画背景
            Rectangle rec = new Rectangle(new Point(0, 0), size);
            g.DrawImage(Properties.Resources.bg, rec);

            //画格子
            int unitColWidth = size.Width / (2 * Cols + 2);
            int unitRowHeight = size.Height / (2 * Rows + 2);

            //画牌
            foreach (var block in Blocks)
            {
                int space = 6;
                Rectangle recBlock = new Rectangle();
                {
                    recBlock.Size = new Size((int)2 * unitColWidth - 2 * space, (int)2 * unitRowHeight - 2 * space);
                    recBlock.Location = new Point((2 * block.Point.Y + 1) * unitColWidth + space, (2 * block.Point.X + 1) * unitRowHeight + space);
                }
                block.Draw(g, recBlock);

            }
        }

        /// <summary>
        /// 处理鼠标点击事件
        /// </summary>
        /// <param name="point"></param>
        /// <param name="size"></param>
        public void MouseDown(Point point, Size size)
        {

            int space = 6;
            int unitColWidth = size.Width / (2 * Cols + 2);
            int unitRowHeight = size.Height / (2 * Rows + 2);
            foreach (var block in Blocks)
            {
                //当前block的长方形
                Rectangle rec = new Rectangle
                {
                    Size = new Size((int)2 * unitColWidth - 2 * space, (int)2 * unitRowHeight - 2 * space),
                    Location = new Point((2 * block.Point.Y + 1) * unitColWidth + space, (2 * block.Point.X + 1) * unitRowHeight + space)
                };

                if (rec.Contains(point))//point这个点在这个矩形里
                {


                    if (block.IsBack == true)//只处理背面朝上的牌
                    {
                        i++;
                        //每次点击鼠标i+1

                        block.MouseDown();
                        //引入block中处理


                        if (i % 2 == 1)
                        {
                            BlockFore = block;
                        }
                        else
                        {
                            if (block.Status == BlockFore.Status)
                            {
                                block.Status = BlockStatus.Blank;
                                BlockFore.Status = BlockStatus.Blank;
                                j++;

                            }
                            //if (block=BlockFore)
                            else
                            {
                                block.IsBack = true;
                                BlockFore.IsBack = true;
                            }
                        }
                    }
                }
            }
            //处理输赢
            if (j == 16)
            {
                IsGameOver = true;
            }
        }
    }
}


